package com.cls.common.message.receive.impl;

import com.cls.business.entity.Message;
import com.cls.business.service.IMessageService;
import com.cls.common.message.RedisReceiveType;
import com.cls.common.message.receive.IRedisMessageReceive;
import com.cls.common.utils.RedisUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 宣传端消息通知
 *
 * @Author Carey
 * @Date: 2020/5/20
 */
@Service
@RequiredArgsConstructor
public class ClientMessageReceiveImpl implements IRedisMessageReceive {

    private final IMessageService messageService;
    private final RedisUtils redisUtils;


    /**
     * 监听消息内容，异步处理消息入库
     * 监听消息中间件消息，存在写库
     *
     * @return
     * @author Carey
     * @date: 2020/5/20 2:58 下午
     */
    @Override
    public void receiveMessage(String message) {

        //根据redis的list中栈的特性，进行判断是否消息已经消费
        Object clientObject = redisUtils.lRightPop(RedisReceiveType.getClientMessage());
        System.out.println("魏起成测试消息通知，消息被接收！redis的index:" + RedisReceiveType.getRedisDatabseIndex());
        //判断消息是否已经被消费
        if (clientObject == null) {
            System.out.println("魏起成测试消息通知，消息已经被消费喽！");
            return;
        }
        System.out.println("魏起成测试消息通知，消息正在被消费！");
        Message sub = (Message) clientObject;
        messageService.createMessage(sub);
    }
}
